// 地图管理器
// L is available globally from leaflet.js
const MapManager = {
    map: null,
    layers: {},
    events: {},

    // 初始化地图
    init: function() {


        this.map = new Map('map', {
            center: [51.505, -0.09],
            zoom: 13
        });

        // 初始化基础图层
        this.addBaseLayer('高德', '道路图');

        // 添加地图加载完成事件
        this.map.whenReady(() => {
            console.log('地图初始化完成');
            if (this.events.ready) {
                this.events.ready.forEach(callback => callback());
            }
        });

        return this;
    },

    // 添加基础图层
    addBaseLayer: function(source, type) {
        this.removeLayer('mssBaseLayer');

        switch(source) {
            case '高德':
                if(type === '卫星图') {
                    this.addLayer(L.tileLayer.chinaProvider('GaoDe.Satellite.Map'), 'mssBaseLayer');
                    this.addLayer(L.tileLayer.chinaProvider('GaoDe.Satellite.Annotion'), 'mssBaseLayer');
                } else {
                    this.addLayer(L.tileLayer.chinaProvider('GaoDe.Normal.Map'), 'mssBaseLayer');
                }
                break;
            // 其他图层类型...
        }
    },

    // 添加图层
    addLayer: function(layer, name) {
        layer.addTo(this.map);
        this.layers[name] = layer;
    },

    // 移除图层
    removeLayer: function(name) {
        if (this.layers[name]) {
            this.map.removeLayer(this.layers[name]);
            delete this.layers[name];
        }
    },

    // 添加事件监听
    on: function(eventName, callback) {
        if (!this.events[eventName]) {
            this.events[eventName] = [];
        }
        this.events[eventName].push(callback);
    }
};

// 初始化地图管理器
const mapManager = MapManager.init();
